<?php


namespace app\admin\controller\system;

use app\admin\controller\BaseController;
use app\common\exception\MessageException;
use app\common\model\mysql\system\User as UserModel;
use app\common\validate\IDMustBePositiveInt;

class User extends BaseController
{
    // 列表所有
    public function list()
    {
        // 分页查询
        $params = $this->request->param();
        $Model = new UserModel();
        $order = [
            'update_time' => 'desc',
            'create_time' => 'desc',
            'id' => 'desc'
        ];
        $query = $Model->order($order);

        $currentPage = $params['current'] ?? 1;
        $pageSize = $params['pageSize'] ?? 30;
        // 筛选
        if (!empty($params['status'])) {
            $query = $query->where('status', '=', $params['status']);
        } else {
            $query = $query->where('status', '<>', 0);
        }

        if (!empty($params['name'])) {
            $query = $query->where('name', 'like', '%' . $params['name'] . '%');
        }

        // 查询该部门下的所有部门id
        if (!empty($params['department_id'])) {
            $query = $query->where('department_id', '=', $params['department_id']);
        }

        // 查询

        $count = $query->count();
        $list = $query->with(['post', 'department'])->page($currentPage, $pageSize)->select();

        return $this->renderSuccess([
            'total' => $count,
            'totalPage' => ceil($count / $pageSize),
            'current' => $currentPage,
            'pageSize' => $pageSize,
            'list' => $list,
        ]);
    }

    public function create()
    {
        if (!$this->request->isPost()) {
            throw new MessageException(['msg' => '请求方法不允许']);
        }
        $data = $this->request->param();
        try {
            $Model = new UserModel();
            // TODO checkMobile checkEmail
//            $name = $Model->checkName($data['name']);
//
//            if ($name) {
//                return $this->renderFail('部门已存在');
//            }

            $result = $Model->create($data);
            return $this->renderSuccess($result, '添加成功');
        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }

    public function update()
    {
        if (!$this->request->isPost()) {
            throw new MessageException(['msg' => '请求方法不合法']);
        }

//        (new ProductCategoryValidate())->goCheck();
        $params = $this->request->param();

        if (!$params['id']) {
            return $this->renderFail('部门不存在');
        }

        try {
            $Model = new UserModel();
            $findData = $Model->checkName($params['name'], $params['id']);
            if ($findData) {
                return $this->renderFail('部门已存在');
            }

            $data = $Model->update($params);

            if ($data) {
                return $this->renderSuccess([], '更新成功');
            }

        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }

    public function delete()
    {
        $params = $this->request->param();
        if (!$params['id']) {
            return $this->renderFail('参数错误');
        }
        if ($params['id'] == 10000) {
            return $this->renderFail('删除失败');
        }

        try {
            $Model = new UserModel();
            $findData = $Model->where('id', $params['id'])->find();

            if (!$findData) {
                return $this->renderFail('部门不存在');
            }

            $data = $Model->update(['id' => $params['id'], 'status' => config('status.mysql.delete')]);
            if ($data) {
                return $this->renderSuccess([], '删除成功');
            }

        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }

    public function detail()
    {
        (new IDMustBePositiveInt())->goCheck();
        $id = $this->request->param('id');
        try {
            $result = UserModel::find($id);
            if ($result) {
                // 查询角色、岗位、部门
//                $department = (new Department())->getTreeList();
                return $this->renderSuccess([
                    'department' => [],
                    'user' => $result
                ]);
            } else {
                return $this->renderFail('用户不存在');
            }
        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }
}